package com.calciominoBeta.Activity;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.calciominoBeta.R;
import com.calciominoBeta.Activity.db.DbSupport;
import com.calciominoBeta.Activity.db.TableDb;
import com.calciominoBeta.Activity.task.Task;
import com.calciominoBeta.Activity.task.TaskType;
import com.calciominoBeta.Adapter.Accueil_Adapter;
import com.calciominoBeta.Entity.InfoOj;
import com.calciominoBeta.Util.Sdata;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.Mlog;

public class AQFragmentVideo extends BaseDetailfm {
	private String TAG = BaseDetailfm.class.getName();
	private TextView tv_title, tv_date_text, tv_author, tv_type, tv_content,
			url_video, html;
	private WebView webView;
	private WebView webtitle;
	private Accueil_Adapter adapter;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected DisplayImageOptions options;
	private ImageView img_share_post;
	ArrayList<InfoOj> dataLv = null;
	ListView lv_accueil;
	protected int itemHeight;

	private InfoOj oj;
	private Context context;
	public String youtube_id;
	ProgressDialog progressDialog;
	//------------------------------------------------
	// em dat dai mot bien o day la id de truyen vao lay video
	//-------------------------------------------------
	//private String mIdVideoPlay;
	//-------------------------------------------------
	
	public static AQFragmentVideo me=null;
	
	public AQFragmentVideo() {
	}

	public static AQFragmentVideo getInstance(InfoOj oj, Context context) {
		AQFragmentVideo aq = new AQFragmentVideo();
		aq.setOj(oj, context);
		return aq;
	}

	private void setOj(InfoOj oj, Context context) {
		this.youtube_id = oj.get(InfoOj.VIDEO_URL);
		this.oj = oj;
		this.context = context;

	}

	View v;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		progressDialog = new ProgressDialog(getActivity());
		progressDialog.show();
		context=getActivity();
		progressDialog.setContentView(R.layout.custom_progress_dialog);
		progressDialog.setCancelable(false);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				progressDialog.dismiss();
			}
		}, 500);
		v = inflater.inflate(R.layout.fm_detail_video, container, false);
		tv_title = (TextView) v.findViewById(R.id.tv_title1);
		tv_date_text = (TextView) v.findViewById(R.id.tv_date_text1);
		tv_type = (TextView) v.findViewById(R.id.tv_type1);
		webView = (WebView) v.findViewById(R.id.webvideo);
		webtitle = (WebView) v.findViewById(R.id.webtitile);
		lv_accueil = (ListView) v.findViewById(R.id.detail_list);
		img_share_post = (ImageView) v.findViewById(R.id.img_share_post);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		me= AQFragmentVideo.this;
		img_share_post.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				share();
			}
		});

		String id = oj.get(InfoOj.ID);
		getData(id);
		adapter = new Accueil_Adapter(getActivity(), imageLoader, options);
		lv_accueil.setAdapter(adapter);

		loadDataObjHome();
		lv_accueil.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				Accueil_Adapter madapter = (Accueil_Adapter) arg0.getAdapter();

				ArrayList<InfoOj> arrNews = madapter.getFull();
				Sdata.dataLv = arrNews;
				Sdata.position = position;
				startActivity(new Intent(getActivity(), AQDetailActivity.class));

			}
		});

	}

	void loadDataObjHome() {

		ArrayList<InfoOj> dataHome = DbSupport.getCache(TableDb.HOME);

		ArrayList<InfoOj> dataHome1 = new ArrayList<InfoOj>();
		for (int i = 0; i < 5; i++) {
			dataHome1.add(dataHome.get(i));
		}
		adapter.setData(dataHome1);
		setListViewHeightBasedOnChildren(lv_accueil);
	}

	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < 5; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
		listView.requestLayout();
	}

	private void getData(String id) {

		ArrayList<String> list = new ArrayList<String>();
		list.add(id);

		BaseModel model = new BaseModel() {
			@Override
			public void onSuccess(int taskType, ArrayList<?> list, String msg) {
				super.onSuccess(taskType, list, msg);
				if (taskType == TaskType.DETAIL) {
					BaseObject ojj = (BaseObject) list.get(0);
					InfoOj infoDetail = new InfoOj();
					infoDetail.setParams(ojj.getParams());

					try {
						tv_title.setText(Html.fromHtml(infoDetail.get(
								InfoOj.TITLE).toUpperCase()));

					} catch (Exception e) {
						return;
					}

					if (infoDetail.get(InfoOj.TYPE).equalsIgnoreCase("breves")) {
						tv_type.setText(R.string.breves);
						tv_type.setTextColor(context.getResources()
								.getColorStateList(R.color.breves));
					}
					if (infoDetail.get(InfoOj.TYPE).equalsIgnoreCase("videos")) {
						tv_type.setText(getString(R.string.video_fr));
						tv_type.setTextColor(context.getResources()
								.getColorStateList(R.color.video));
					}
					if (infoDetail.get(InfoOj.TYPE)
							.equalsIgnoreCase("articles")) {
						tv_type.setText("ARTICLES");
						tv_type.setTextColor(context.getResources()
								.getColorStateList(R.color.articles));
					}

					tv_title.setTextColor(context.getResources()
							.getColorStateList(R.color.detail_title));
					tv_date_text.setText(infoDetail.get(InfoOj.DATE_TEXT));
					String mhtml = "<html><head><style type=\"text/css\">@font-face {font-family: 'arial';src: url(\"file:///android_asset/font/fontawesome-webfont.ttf\")}body {font-family: 'arial';font-size: medium;text-align: justify;}line-height:20px;</style></head><body>";
					String nhtml = "</body></html>";
					String mcontent = mhtml + infoDetail.get(InfoOj.CONTENT)
							+ nhtml;

					webtitle.loadDataWithBaseURL("", mcontent, "text/html",
							"utf-8", "");
					String pish = "https://www.youtube.com/watch?v=";

					String myHtmlString = pish + youtube_id;
					webView.setWebViewClient(new WebViewClient());
					webView.setWebChromeClient(new WebChromeClient());
					webView.getSettings().setJavaScriptEnabled(true);
					webView.getSettings().setPluginState(
							WebSettings.PluginState.ON);
					webView.loadUrl(myHtmlString);
					lv_accueil
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> arg0,
										View arg1, int position, long arg3) {

									Accueil_Adapter madapter = (Accueil_Adapter) arg0
											.getAdapter();

									ArrayList<InfoOj> arrNews = madapter
											.getFull();
									Sdata.dataLv = arrNews;
									Sdata.position = position;
									startActivity(new Intent(getActivity(),
											AQDetailActivity.class));

								}
							});
				}
			}
			@Override
			public void onFail(int taskType, String msg) {
				// TODO Auto-generated method stub
				super.onFail(taskType, msg);
			}
		};
		new Task(model, TaskType.DETAIL, list, getActivity()).exe();

	}

	void share() {
		InfoOj oj = new InfoOj();
		String title = oj.get(oj.get(InfoOj.TITLE));
		Intent share = new Intent(android.content.Intent.ACTION_SEND);
		share.setType("text/plain");
		share.putExtra(Intent.EXTRA_TEXT, title);
		startActivity(Intent.createChooser(share, "Calciomino"));
	}

	public void dm(){
		webView.setVisibility(View.GONE);
	}
	@Override
	public void onPauseVideo(String idVideo) {
		// TODO Auto-generated method stub
		super.onPauseVideo(idVideo);
		// TODO Auto-generated method stub
		if( youtube_id.equals(idVideo) ){
			Log.e(TAG ,"[AQFragmentVideo]-onPauseVideo  PAUSE VIDEO");	
		}else{
			if(webView != null){
				webView.loadUrl("http://www.google.com.vn/");
			}			
			Log.e(TAG ,"[AQFragmentVideo]-onPauseVideo  START VIDEO");
		}
	}
	
//	@Override
//	public void onPauseVideo() {
//		webView.setVisibility(View.GONE);
//		super.onPauseVideo();
//	}

}
